//
//  RegisterViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/27.
//  Copyright © 2016年 KX. All rights reserved.
//
#import "APPTools.h"
#import "RegisterViewController.h"
#import "Masonry.h"
#import "FreeTravelMacro.h"
#import "FreeTravelNetwork.h"
#import "VerificationCodeViewController.h"
@interface RegisterViewController ()
@property (nonatomic, strong) UITextField *phoneTextField;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationItem.title = @"手机号注册";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.view.backgroundColor = [UIColor colorWithRed:0.11 green:0.68 blue:0.45 alpha:1.00];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sdfasadfas"] style:UIBarButtonItemStylePlain target:self action:@selector(goLogin)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.phoneTextField = [[UITextField alloc]init];
    [self.view addSubview:_phoneTextField];
    _phoneTextField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"填写您的手机号" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.centerY.mas_equalTo(self.view).multipliedBy(0.4);
        make.height.mas_equalTo(self.view).multipliedBy(0.08);
        make.width.mas_equalTo(self.view).multipliedBy(0.8);
    }];
    
    UIView *firstWhiteLine = [[UIView alloc]init];
    firstWhiteLine.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:firstWhiteLine];
    [firstWhiteLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.equalTo(self.phoneTextField.mas_bottom);
        make.height.mas_equalTo(2);
        make.width.mas_equalTo(self.view).multipliedBy(0.8);
    }];
    
    UIButton *registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:registButton];
    [registButton setTitle:@"下一步" forState:UIControlStateNormal];
    [registButton setTitleColor:COLORFORFREETRAVEL forState:UIControlStateNormal];
    registButton.backgroundColor = [UIColor whiteColor];
    registButton.layer.cornerRadius = 5;
    registButton.layer.masksToBounds = YES;
    [registButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(44);
        make.center.mas_equalTo(self.view);
    }];
    [registButton addTarget:self action:@selector(goNext) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)goNext{
    // 判断一下手机号格式, 符合的话再请求
//    NSLog(@"%@",[self.phoneTextField.text substringToIndex:1] );
    if (self.phoneTextField.text.length == 11 && [[self.phoneTextField.text substringToIndex:1]  isEqual: @"1"]) {
                [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/user/active_code?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_code=86&mobile=%@&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&v=1",self.phoneTextField.text] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
            NSLog(@"~~~~~~~%@",result);
                    if ([[result objectForKey:@"status"] isEqual:@1]) {
                        VerificationCodeViewController *verificationCode = [[VerificationCodeViewController alloc]init];
                        verificationCode.phoneNumber = self.phoneTextField.text;
                        [self.navigationController pushViewController:verificationCode animated:YES];
                    }else if([[result objectForKey:@"status"] isEqual:@20314]){
                        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"手机号已存在" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                            
                        }];
                        
                        
                        [alertVC addAction:alertAction];
                        [self presentViewController:alertVC animated:YES completion:^{
                            
                        }];
                    }
                    
            
            
        } andFailureBlock:^(NSError *error) {
            
        }];
    }else{
        NSLog(@"请输入正确的手机号");
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"请输入正确的手机号" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *certain = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alertVC addAction:alertAction];
        [alertVC addAction:certain];
        [self presentViewController:alertVC animated:YES completion:^{
            
        }];
        
    }
    
    
    
}
- (void)goLogin{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
